"use client"
import { create } from "zustand"
import { v4 as uuidv4 } from "uuid"

// 定义 AI 服务商类型
export type ApiProvider = "Deepseek" | "SiliconFlow"

// 定义 DeepSeek 模型类型
export type DeepSeekModel = "deepseek-chat" | "deepseek-reasoner"

// 定义消息类型
export interface Message {
	id: string
	role: "user" | "assistant"
	content: string
	thinking?: string // 思考过程内容
}

// 应用状态接口定义
interface AppState {
	messages: Message[]
	selectedApi: ApiProvider
	selectedDeepSeekModel: DeepSeekModel
	isLoading: boolean
	error: string | null
	selectedFile: File | null
	isUploading: boolean
	uploadError: string | null
	// 侧栏状态
	sidebarOpen: boolean
	sidebarContent: string | null
	sidebarTitle: string
	addMessage: (role: "user" | "assistant", content: string, thinking?: string) => string // 返回新消息的 ID
	updateMessage: (id: string, content: string, thinking?: string, replace?: boolean) => void // 根据 ID 更新消息内容
	setSelectedApi: (api: ApiProvider) => void
	setSelectedDeepSeekModel: (model: DeepSeekModel) => void
	setIsLoading: (isLoading: boolean) => void
	setError: (error: string | null) => void
	setSelectedFile: (file: File | null) => void
	setIsUploading: (isUploading: boolean) => void
	setUploadError: (error: string | null) => void
	// 侧栏控制方法
	setSidebarOpen: (open: boolean) => void
	setSidebarContent: (content: string | null, title?: string) => void
}

// 创建 Zustand Store
const useAppStore = create<AppState>(set => ({
	messages: [
		{
			id: uuidv4(),
			role: "assistant",
			content: "你好！我是你的 AI 排版助手。请将你的文章内容或排版指令发送给我，我将为你提供帮助。你也可以上传Word文档，我会帮你转换为HTML格式。"
		}
	],
	selectedApi: "Deepseek", // 默认选择 Deepseek
	selectedDeepSeekModel: "deepseek-reasoner", // 默认选择思考模式
	isLoading: false,
	error: null,
	selectedFile: null,
	isUploading: false,
	uploadError: null,
	// 侧栏状态
	sidebarOpen: false,
	sidebarContent: null,
	sidebarTitle: "HTML 预览",
	addMessage: (role, content, thinking) => {
		const newMessage = { id: uuidv4(), role, content, thinking }
		set(state => ({
			messages: [...state.messages, newMessage]
		}))
		return newMessage.id
	},
	updateMessage: (id, content, thinking, replace = false) => {
		set(state => ({
			messages: state.messages.map(msg =>
				msg.id === id
					? {
							...msg,
							content: replace ? content : msg.content + content,
							thinking: thinking !== undefined ? (msg.thinking || "") + thinking : msg.thinking
					  }
					: msg
			)
		}))
	},
	setSelectedApi: api => set({ selectedApi: api }),
	setSelectedDeepSeekModel: model => set({ selectedDeepSeekModel: model }),
	setIsLoading: isLoading => set({ isLoading }),
	setError: error => set({ error }),
	setSelectedFile: file => set({ selectedFile: file }),
	setIsUploading: isUploading => set({ isUploading }),
	setUploadError: error => set({ uploadError: error }),
	// 侧栏控制方法
	setSidebarOpen: open => set({ sidebarOpen: open }),
	setSidebarContent: (content, title = "HTML 预览") =>
		set({
			sidebarContent: content,
			sidebarTitle: title,
			sidebarOpen: content !== null
		})
}))

export default useAppStore
